package com.yunqian.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yunqian.constant.ZPTConfig;
import com.yunqian.util.validate.Validator;
import lombok.extern.log4j.Log4j2;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author czy
 * @describe
 * @date 2018/10/26
 */
@Log4j2
public class ZPTUtil {


    /**
     * 下单接口
     */
    public static String createOrder(String srcOrderID, String ticketTypeCode, String useDate, String number, String price) {

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 请求时间
        String requestTime = dateFormat.format(new Date());
        // 签名
        String sign = MD5Util.encode(ZPTConfig.APINAME_CREATEORDER + ZPTConfig.ACCOUNTID + ZPTConfig.VER + requestTime + ZPTConfig.KEY);
        // 请求体
        Map<String, Map<String, String>> map = new HashMap<>(16);
        Map<String, String> head = new HashMap<>(16);
        Map<String, String> body = new HashMap<>(16);
        head.put("apiName", ZPTConfig.APINAME_CREATEORDER);
        head.put("accountId", ZPTConfig.ACCOUNTID);
        head.put("ver", ZPTConfig.VER);
        head.put("sign", sign);
        head.put("requestTime", requestTime);
        body.put("srcOrderID", srcOrderID);
        body.put("ticketTypeCode", ticketTypeCode);
        body.put("useDate", useDate);
        body.put("number", number);
        body.put("price", price);
        map.put("head", head);
        map.put("body", body);
        String jsonStr = JSON.toJSONString(map);
        String result = HttpUtil.httpRequest(ZPTConfig.URL, "POST", jsonStr);
        log.warn("123254646" + result);
        try {

            if (Validator.isNotNull(result)) {
                JSONObject obj = JSONObject.parseObject(result);
                String resultId = obj.getJSONObject("head").getString("resultCode");
                if (Validator.equals(resultId, "1")) {
                    return result;
                }

            }
        }catch (Exception e){
            log.warn("众票通下单失败，订单号：" + srcOrderID);
        }
        return null;
    }

    /**
     * 取消订单
     *
     * @param orderID
     * @param srcOrderID
     * @param number
     * @param cancelOrderID
     */
    public static Integer cancelOrder(String orderID, String srcOrderID, String number, String cancelOrderID) {

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 请求时间
        String requestTime = dateFormat.format(new Date());
        // 签名
        String sign = MD5Util.encode(ZPTConfig.APINAME_CANCELORDER + ZPTConfig.ACCOUNTID + ZPTConfig.VER + requestTime + ZPTConfig.KEY);
        // 请求体
        Map<String, Map<String, String>> map = new HashMap<>(16);
        Map<String, String> head = new HashMap<>(16);
        Map<String, String> body = new HashMap<>(16);
        head.put("apiName", ZPTConfig.APINAME_CANCELORDER);
        head.put("accountId", ZPTConfig.ACCOUNTID);
        head.put("ver", ZPTConfig.VER);
        head.put("sign", sign);
        head.put("requestTime", requestTime);
        body.put("orderID", orderID);
        body.put("srcOrderID", srcOrderID);
        body.put("number", number);
        body.put("cancelOrderID", cancelOrderID);
        map.put("head", head);
        map.put("body", body);
        String jsonStr = JSON.toJSONString(map);
        String result = HttpUtil.httpRequest(ZPTConfig.URL, "POST", jsonStr);
        if (Validator.isNotNull(result)) {
            JSONObject obj = JSONObject.parseObject(result);
            String resultCode = obj.getJSONObject("head").getString("resultCode");
            if (Validator.equals(resultCode, "1")) {
                // 状态码
                Integer state = Integer.valueOf(obj.getJSONObject("body").getString("state"));
                return state;
            }
        }
        log.warn("zpt取消订单失败，订单ID：{}，原因:{}", orderID, result);
        return 0;
    }

    /**
     * 查询订单
     *
     * @param orderID
     * @param srcOrderID
     */
    public void queryOrder(String orderID, String srcOrderID) {

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 请求时间
        String requestTime = dateFormat.format(new Date());
        // 签名
        String sign = MD5Util.encode(ZPTConfig.APINAME_QUERYORDER + ZPTConfig.ACCOUNTID + ZPTConfig.VER + requestTime + ZPTConfig.KEY);
        // 请求体
        Map<String, Map<String, String>> map = new HashMap<>(16);
        Map<String, String> head = new HashMap<>(16);
        Map<String, String> body = new HashMap<>(16);
        head.put("apiName", ZPTConfig.APINAME_QUERYORDER);
        head.put("accountId", ZPTConfig.ACCOUNTID);
        head.put("ver", ZPTConfig.VER);
        head.put("sign", sign);
        head.put("requestTime", requestTime);
        body.put("orderID", orderID);
        body.put("srcOrderID", srcOrderID);
        map.put("head", head);
        map.put("body", body);
        String jsonStr = JSON.toJSONString(map);
        String result = HttpUtil.httpRequest(ZPTConfig.URL, "POST", jsonStr);
        if (Validator.isNotNull(result)) {
            JSONObject obj = JSONObject.parseObject(result);
            String resultId = obj.getJSONObject("head").getString("resultId");
            if (Validator.equals(resultId, "1")) {
                // 状态码
                String orderID_ = obj.getJSONObject("body").getString("orderID");
                // 备注
                String srcOrderID_ = obj.getJSONObject("body").getString("srcOrderID");
                String orderQty = obj.getJSONObject("body").getString("orderQty");
                String usedQty = obj.getJSONObject("body").getString("usedQty");
                String refundQty = obj.getJSONObject("body").getString("refundQty");
                String code = obj.getJSONObject("body").getString("code");
                String codeURL = obj.getJSONObject("body").getString("codeURL");
            }
        }
    }

    /**
     * 获取票类接口
     */
    public static void getTicketList() {

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 请求时间
        String requestTime = dateFormat.format(new Date());
        // 签名
        String sign = MD5Util.encode(ZPTConfig.APINAME_GETTICKETLIST + ZPTConfig.ACCOUNTID + ZPTConfig.VER + requestTime + ZPTConfig.KEY);
        // 请求体
        Map<String, Map<String, String>> map = new HashMap<>(16);
        Map<String, String> head = new HashMap<>(16);
        head.put("apiName", ZPTConfig.APINAME_GETTICKETLIST);
        head.put("accountId", ZPTConfig.ACCOUNTID);
        head.put("ver", ZPTConfig.VER);
        head.put("sign", sign);
        head.put("requestTime", requestTime);
        map.put("head", head);
        String jsonStr = JSON.toJSONString(map);
        String result = HttpUtil.httpRequest(ZPTConfig.URL, "POST", jsonStr);
        System.out.println(result);
    }


    /**
     * 核销回调
     *
     * @param request
     * @return
     */
    public static String callBack(HttpServletRequest request) {
        try {
            BufferedReader reader;
            reader = request.getReader();
            String line;
            String xmlString;
            StringBuffer inputString = new StringBuffer();
            while ((line = reader.readLine()) != null) {
                inputString.append(line);
            }
            // 接收到的数据
            xmlString = inputString.toString();
            request.getReader().close();
            return  xmlString;
        } catch (Exception e) {
            log.warn("众票通回调数据解析异常", e);
        }
        return null;
    }

}
